package com.lanp.springmongodb.service;

import com.lanp.springmongodb.dao.m1.Comment1Dao;
import com.lanp.springmongodb.dao.m2.Comment2Dao;
import com.lanp.springmongodb.model.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class CsImpl implements Cs{

    @Autowired
    private Comment1Dao commentDao;
    @Autowired
    private Comment2Dao commentDaoM2;

    /**
     * 保存一个评论
     *
     * @param comment
     */
    public void saveComment(Comment comment) {
        //如果需要自定义主键，可以在这里指定主键；如果不指定主键，MongoDB会自动生成主键
//        commentDao.save(comment);

        comment.setContent(UUID.randomUUID().toString());
        comment.setUserid("m1");
        commentDao.save(comment);

        comment.setContent(UUID.randomUUID().toString());
        comment.setUserid("m2");
        commentDaoM2.save(comment);
    }

    /**
     * 更新评论
     *
     * @param comment
     */
    public void updateComment(Comment comment) {
        commentDao.save(comment);
    }

    /**
     * 根据id删除评论
     *
     * @param id
     */
    public void deleteCommentById(String id) {
        commentDao.deleteById(id);
    }

    /**
     * 查询所有评论
     *
     * @return
     */
    public List<Comment> findCommentList() {
        return commentDao.findAll();
    }

    /**
     * 根据id查询评论
     *
     * @param id
     * @return
     */
    public Comment findCommentById(String id) {
        return commentDao.findById(id).get();
    }
}

